package org.jeecg.modules.oa.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.OaJournal;
import org.jeecg.modules.oa.entity.OaReportWeek;
import org.jeecg.modules.oa.mapper.OaJournalMapper;
import org.jeecg.modules.oa.mapper.OaReportWeekMapper;
import org.jeecg.modules.oa.service.IOaJournalService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 工作日志表
 * @Author: jeecg-boot
 * @Date:   2019-09-11
 * @Version: V1.0
 */
@Service
public class OaJournalServiceImpl extends ServiceImpl<OaJournalMapper, OaJournal> implements IOaJournalService {
    @Autowired
    private OaJournalMapper journalMapper;

    @Autowired
    private OaReportWeekMapper reportWeekMapper;

    @Override
    public Page<OaJournal> listByUserPage(Page<OaJournal> page, OaJournal oaJournal) {
        List<OaJournal> list = journalMapper.listByUser(page, oaJournal);
        return page.setRecords(getImageListAndCurrentUserId(list));
    }

    @Override
    public List<OaJournal> findListByDateList(SysUser user, List<Date> dateList) {
        return getImageListAndCurrentUserId(journalMapper.findListByDateList(user,dateList));
    }

    @Override
    public OaJournal findByUserDate(OaJournal journal) {
        return journalMapper.findByUserDate(journal);
    }

    @Override
    public Page<OaJournal> findPcAllList(Page<OaJournal> page, OaJournal oaJournal,String departIds) {
        List<OaJournal> list = journalMapper.findPcAllList(page, oaJournal,departIds);
        return page.setRecords(getImageListAndCurrentUserId(list));
    }

    @Override
    public Page<OaJournal> find(Page<OaJournal> page, OaJournal journal) {
        List<OaJournal> list = journalMapper.findAllList(page, journal);
        return page.setRecords(getImageListAndCurrentUserId(list));
    }

    @Override
    public List<OaJournal> findAllUnread(OaJournal journa) {
           // journal.getSqlMap().put("dsf", dataScopeFilterExact(journal.getCurrentUser(), "o", "u", "oa:journal:view"));
            List<OaJournal> journalList = journalMapper.findAllList(null,journa);
            return journalList;
    }

    @Override
    public List<OaJournal> findAllListToOutSource(OaJournal journal) {
        return journalMapper.findAllListToOutSource(journal);
    }

    @Override
    public OaJournal getByCreateByAndToday(String id) {
        return journalMapper.getByCreateByAndToday(id);
    }

    @Override
    public List<OaJournal> appFindJournalList(String date,String userId) {
        return journalMapper.appFindJournalList(date,userId);
    }

    @Override
    public List<OaReportWeek> appFindReportWeekList(String date,String userId) {
        return reportWeekMapper.appFindReportWeekList(date,userId);
    }

    private List<OaJournal> getImageListAndCurrentUserId(List<OaJournal> list){
        if(CollectionUtil.isNotEmpty(list)){
            LoginUser user = SsoUtil.getLoginUser();
            for (OaJournal oj : list) {
                oj.setCurrentUserId(user.getId());
                boolean sameDate = DateUtils.isSameDate(new Date(), oj.getCreateTime());
                oj.setIsNewest(sameDate);
                if (oj.getImageUrl() != null && StringUtils.isNotEmpty(oj.getImageUrl().toString())) {
                    String[] imgs = oj.getImageUrl().toString().split(";");
                    if (imgs.length > 0)
                        oj.setImageUrlList(Arrays.asList(imgs));
                }
            }
        }
        return list;
    }

}
